package com.hhsjdesign.dynamicui.utils

import org.json.JSONArray
import org.json.JSONObject


/**
 * 我的设计是 屏幕宽度 360等分
 */
const val SCREEN_WIDTH = 1080
const val RATIO = SCREEN_WIDTH / 360f

/**
 * 解析
 */
fun JSONObject.optDimen(key: String, defaultValue: Int = 0): Int {
    var result = this.optInt(key, defaultValue)
    if (result == 0 || result == -1 || result == -2) {
        return result
    }
    return (result * RATIO).toInt()
}


fun JSONArray.optDimen(index: Int, defaultValue: Int = 0): Int {
    var result = this.optInt(index, defaultValue)
    if (result == 0 || result == -1 || result == -2) {
        return result
    }
    return (result * RATIO).toInt()
}

fun designUnit2px(designUnit: Int): Int {
    return (designUnit * RATIO).toInt()
}

fun makeTag(tag: String): String = "hhsj_${tag}"